// miniprogram/pages/book/book.js
const APP=getApp()
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    status:1,
    date_list:[],//存储日期列表
    current_select_date:0,
    current_select_time:undefined,
    maximum:30,
    time_list:[
      {booking_number:0,
        booking_status:"",
        isOpen:true,
       specific_time:"早上 6:00~9:00  "},
       {booking_number:0,
         booking_status:"",
         isOpen:true,
       specific_time:"早上 9:00~12:00"},
       {booking_number:0,
         booking_status:"",
         isOpen:true,
       specific_time:"下午 12:00~15:00"},
       {booking_number:0,
        booking_status:"",
        isOpen:true,
      specific_time:"下午 15:00~18:00"},
      {booking_number:0,
        booking_status:"",
        isOpen:true,
      specific_time:"晚上 18:00~22:00"}
    ],
    switch1Checked: false,
  },
  switch1Change:function(e){
    let that=this;
    let left=' time_list['+that.data.current_select_time+'].isOpen';
    if (this.data.current_select_time==undefined){
      wx.showToast({
        icon:'none',
        title: '请选择需要包场的时间段',
      })
      this.setData({
        switch1Checked:false
      })
    }else if (e.detail.value){
      wx.showModal({
    
        title:'确定要包场吗？',
        editable:true,
        placeholderText:'请输入包场理由',
        success(res){
          if (res.confirm){
            let message=res.content;
            let option=!e.detail.value;     
            that.book_whole(option,message)
            that.setData({
              switch1Checked:e.detail.value,
               [left]:false
            })
            console.log(that.data.time_list)
            console.log(res)
          }else{
            that.setData({
              switch1Checked:false
            })
          }
        }
      })
    }else {
      wx.showModal({
       
        title:'确定要取消包场吗？',
        success(res){
          if(res.confirm){
            let message="";
            let option=!e.detail.value;  
            that.book_whole(option,message)
            that.setData({
              switch1Checked:e.detail.value,
               [left]:true
            })
          }else{
            that.setData({
              switch1Checked:true
            })
          }
        }
      })
    }
    // console.log(this.data.switch1Checked)
    console.log(this.data.time_list)
  },
book_whole:function(option,message){
  let that=this;
  // console.log("1",option)
  // console.log("2",message)
  // console.log(that.data.time_list[that.data.current_select_time].timestamp)
  wx.cloud.callFunction({
		name:"timeTable",
		data:{
			type:"setOpen",
      //timestap: that.data.time_list[that.data.current_select_time].timestamp, // 这个是你自己的变量名写错了
      timestamp:that.data.time_list[that.data.current_select_time].timestamp,
			option:option,
		  message:message, 
			},
		success(res)
		{
      console.log(res)
      wx.showToast({
        icon:'none',
        title: res.result.msg,
      })
		},
		fail(res)
		{
      console.log(res)
			wx.showToast({
			   icon: 'none',
	           title: '云函数调用失败',
			})
		}
	})
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that=this;
    let G_status=APP.globalData.user.status; //获取用户权限
    let list=this.getDates(8) //获取后8天日期
    list.splice(0,1); //去掉今天
    this.setData({
      date_list:list, 
      status:G_status
    })
    console.log(this.data.date_list)
    this.GetTimeTable(0);
  },
GetTimeTable:function(date_id){
  let that=this;
  let d = new Date();
  let startTimestamp = d.setHours(0,0,0,0)+86400000  //明天的日期 
  let endTimestamp = d.setHours(24) + 691200000   // 八天后日期
  wx.cloud.callFunction({
    name:"timeTable",
    data:{
      type:"getTimeTable",
      startTimestamp:startTimestamp,
      endTimestamp:endTimestamp,
      },
    success(res)
    {
      console.log(res)
      if (res.result.code==200){
          let data = res.result.data
           console.log(data)
           that.setData({
             time_booking_detail:data
           })
           that.update_time_list(date_id)
      }else{
        wx.showToast({
          icon: 'none',
          title:res.result.msg
        })
      }
     
    },
    fail(res)
    {
      wx.showToast({
         icon: 'none',
             title: '云函数调用失败',
      })
    }
  })
  console.log(this.data)
},
//获取当前时间
getCurrentMonthFirst: function () {
  var date = new Date();
  var todate = date.getFullYear() + "-" + ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1) + "-" + (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate());
  return todate;
},
getDates: function (days, todate = this.getCurrentMonthFirst()) {
  var dateArry = [];
  for (var i = 0; i < days; i++) {
    var dateObj = util.dateLater(todate, i);
    dateArry.push(dateObj)
  }
  return dateArry;
},
select_date:function(e){ //更新所选日期
  // console.log(e)
  let id=e.target.dataset.id;
  // console.log(id)
  this.update_time_list(id)
},
update_time_list:function(id){ //更新所选日期的时间段详细信息显示
  let new_time_list=this.data.time_list
  for (var i=0; i<5; i++){  //时间段数量
      new_time_list[i].booking_number=this.data.time_booking_detail[id*5+i].number;
      new_time_list[i].isOpen=this.data.time_booking_detail[id*5+i].isOpen;
      new_time_list[i].timestamp=this.data.time_booking_detail[id*5+i].timestamp;
      if (this.data.time_booking_detail[id*5+i].isOpen==false) {new_time_list[i].booking_status="不开放";}
      else if (this.data.time_booking_detail[id*5+i].number>=30) {new_time_list[i].booking_status="已满";}else {
        new_time_list[i].booking_status="";
      }
     // new_time_list[i].booking_status=this.data.time_booking_detail[id*5+i].message;
  }
  this.setData({
    current_select_date:id,
    time_list:new_time_list
  })
},
select_time:function(e){ //更新所选时间段
  // console.log(e)
  let id=e.target.dataset.id;
  // console.log(id)
  this.setData({
    current_select_time:id,
    switch1Checked: !this.data.time_list[id].isOpen
  })
},
bindFormSubmit:function(e){
  // console.log(e)
  let that=this;
  let reason=e.detail.value.reason;
  /**信息汇总**/
  // console.log(this.data.date_list[this.data.current_select_date])
  // console.log(this.data.time_list[this.data.current_select_time])
  let time=this.data.time_list[this.data.current_select_time];
  let timestamp=this.data.time_list[this.data.current_select_time].timestamp;
  // console.log(reason)
  // console.log(e.detail.value)
  if (time!=undefined && e.detail.value.reason!=""){
    /** 发送申请 */
    wx.showModal({
  
      content:'确认要预约该时间段吗',
      success(res){
        if (res.confirm){
          console.log(timestamp)
          wx.cloud.callFunction({
            name:"order",
            data:{
              type:"setOrder",
              timestamp:timestamp,
              reason:reason,
              option: 1
            },
            success(res){
              that.GetTimeTable(that.data.current_select_date); //刷新时间表
              console.log(res)
              if (res.result!=null){
              wx.showToast({
                icon:'none',
                title: res.result.msg,
              }) }
            },
            fail(res){
              console.log('云函数调用失败',res)
            }	
          })
        }
      }
    })
  }
  else if (time==undefined){
    wx.showToast({
      title: '请选择时间段',
      icon:'error',
      duration:2000
    })
  } else if(e.detail.value.reason==""){
    wx.showToast({
      title: '理由不能为空！',
      icon:'error',
      duration:2000
    })
  }
  /**弹窗确认 */
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})